package com.buba.service.impl;

import com.buba.mapper.*;
import com.buba.service.InformationService;
import entity.FollowUserEntiry;
import entity.KudosEntity;
import entity.VO.InformationVO;
import entity.VO.ReleaseTopicVO;
import entity.VO.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 孔德嘉
 * @Description: TODO资讯
 * @DateTime: 2023/3/4 15:04
 **/
@Service
public class InformationServiceImpl implements InformationService {
    //注入
    @Autowired
    private InformationMapper informationMapper;
    @Autowired
    private FileMapper fileMapper;
    @Autowired
    private DailyRecordMapper dailyRecordMapper;
    @Autowired
    private KudosMapper kudosMapper;
    @Autowired
    private FollowUserMapper followUserMapper;
    /*
     * @param id //用户id
     * @return list
     * @author kdj
     * @describe:资讯全查列表
     * @date 2023/3/4 15:05
     */
    @Override
    public List<Map> getInformationLabel(int id) {
        //定义一个List用于存放内容
        List<Map> list=new ArrayList<>();

        //全查出资讯表
        List<InformationVO> informationLabel = informationMapper.getInformationList();
        for (InformationVO informationVO : informationLabel) {
            //根据咨询表查出相对应的发布用户信息
            UserVO userVO = informationMapper.getUserById(informationVO.getInformationUserId());
            //根据资讯id查出相对的图片path
            String fileId = fileMapper.getFileId(informationVO.getInformationId());
            //查询到评论数
            String commentNum = dailyRecordMapper.getCommentNum(informationVO.getInformationId(), "3");
            //查询到点赞数
            int kudosNum = kudosMapper.getKudosNum(informationVO.getInformationId(), 4);
            //查询该用户是否点赞过
            KudosEntity kudosById = kudosMapper.getKudosById(informationVO.getInformationId(), 4, id);
            //定义变量 判断是否点赞
            Boolean isKudosByUserId;
            //判断KudosEntity是否为空，kudos_whether是否为0
            if(kudosById==null || kudosById.getKudosWhether()==1){
                //没点赞
                isKudosByUserId=false;
            }else{
                //点赞了
                isKudosByUserId=true;
            }
            //获取点赞列表
            List<UserVO> kudosUserList = kudosMapper.getKudosUserList(informationVO.getInformationId(), 4,false);
            //定义点赞头像列表
            ArrayList<String> kudosUserFileList = new ArrayList<>(kudosUserList.size());
            //获取用户头像
            for (UserVO vo : kudosUserList) {
                //添加到数组里面
                kudosUserFileList.add(vo.getUserFilePath());
            }
            //根据信息id找到他的topic标签
            String labelName = informationMapper.getTopicById(informationVO.getInformationId()).getInformationTopicTitles();
            //判断是否关注了该用户
            FollowUserEntiry followByUserId = followUserMapper.getFollowByUserId(id, userVO.getUserId());
            //判断
            //定义变量 判断是否点赞
            Boolean isFollow;
            if(followByUserId==null || followByUserId.getDelFlag()==1){
                //没点赞
                isFollow=false;
            }else{
                //点赞了
                isFollow=true;
            }
            //创建Map
            Map<String, Object> map = new HashMap<>();
            //存入到map当中
            map.put("type",4);//日记为1 话题为2 资讯为3
            map.put("userId",userVO.getUserId());//发布资讯的用户id
            map.put("userName",userVO.getUserName());//用户名称
            map.put("userFilePath",userVO.getUserFilePath());//用户头像
            map.put("id",informationVO.getInformationId());//内容id
            map.put("contentFilePath",fileId);//内容图片（纸需要第一个）
            map.put("caption",informationVO.getInformationTitle());//标题
            map.put("browse",informationVO.getInformationRecordBrowse());//浏览
            map.put("labelName",labelName);//标签
            map.put("commentNum",commentNum);//评论数
            map.put("kudosNum",kudosNum);//点赞数
            map.put("kudosUserFileList",kudosUserFileList);//点赞列表
            map.put("createTime",informationVO.getCreateTime());//创建时间
            map.put("isKudosByUserId",isKudosByUserId);//是否点赞
            map.put("isFollow",isFollow);//是否点赞
            //将map添加到list当中
            list.add(map);
        }

        return list;
    }

}
